package com.liupengyuan.framework.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class ResponseEncodeFilter implements Filter {
	private FilterConfig config = null;
	private String targetEncoding = "UTF-8";
	
	public void destroy() {
		this.targetEncoding = null;
		this.config = null;
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		response.setContentType("text/html;charset=" + this.targetEncoding);
		response.setCharacterEncoding(targetEncoding);
		 chain.doFilter(request, response);
	}

	public void init(FilterConfig config) throws ServletException {
		this.config = config;
		String targetEncoding = this.config.getInitParameter("encoding");
		
		if ((targetEncoding != null) && (targetEncoding.trim().length() > 0))
			this.targetEncoding = targetEncoding;
	}


}
